﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace Arduino_PDE_to_VScxProj_converter
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main( string[] args)
        {
            try
            {
            if(args.Length != 1)
           {
               return;

           }

           FileInfo inoFile = new FileInfo(args[0]);

            if(inoFile.Exists)
            {
                string projectName = inoFile.Name.Substring(0, inoFile.Name.Length - 4);
                DirectoryInfo newDir = inoFile.Directory.CreateSubdirectory(projectName);
                
                CreateVCXProjStructure(newDir,projectName);

               

                IEnumerable<FileInfo> sourceFiles = inoFile.Directory.GetFiles("*.cpp");
                foreach (var file in sourceFiles)
                {
                    file.CopyTo(Path.Combine(newDir.FullName, file.Name),true);
                }


                IEnumerable<FileInfo> headerFiles = inoFile.Directory.GetFiles("*.h");
                foreach (var file in headerFiles)
                {
                    file.CopyTo(Path.Combine(newDir.FullName, file.Name),true);
                }



                IEnumerable<FileInfo> pdeFiles = inoFile.Directory.GetFiles("*.pde");
                foreach (var file in pdeFiles)
                {
                    file.CopyTo(Path.Combine(newDir.FullName, file.Name), true);
                }

                IEnumerable<FileInfo> inoFiles = inoFile.Directory.GetFiles("*.ino");
                foreach (var file in pdeFiles)
                {
                    file.CopyTo(Path.Combine(newDir.FullName, file.Name), true);
                }


                var newInoFile = inoFile.CopyTo(Path.Combine(newDir.FullName, inoFile.Name), true);
               
                UpdateInoFile(newInoFile, pdeFiles,inoFiles);


                UpdateVCXProjFile(Path.Combine(newDir.FullName, projectName + ".vcxproj"), headerFiles, sourceFiles, pdeFiles,inoFiles);

            }
            MessageBox.Show("Done !");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            } 
        }

        private static void UpdateVCXProjFile(string vcxProjFilePath, IEnumerable<FileInfo> headerFiles, IEnumerable<FileInfo> sourceFiles, IEnumerable<FileInfo> pdeFiles, IEnumerable<FileInfo> inoFiles)
        {
            
            ArduinoVCXProjUpdater.Program.UpdateVCXProjFile(new FileInfo(vcxProjFilePath));
            
            
            // Create the node list... Yes, i'm lazy... no XMLDoc or Xpath here. :-)
            string nodeList = "";
            foreach (var sourceFile in sourceFiles)
            {
                nodeList += "\t<None Include=\"" + sourceFile.Name + "\" /> " + Environment.NewLine;
            }
            foreach (var sourceFile in headerFiles)
            {
                nodeList += "\t<None Include=\"" + sourceFile.Name + "\" /> " + Environment.NewLine;
            }

            foreach (var sourceFile in pdeFiles)
            {
                nodeList += "\t<None Include=\"" + sourceFile.Name + "\" /> " + Environment.NewLine;
            }


            foreach (var sourceFile in inoFiles)
            {
                nodeList += "\t<None Include=\"" + sourceFile.Name + "\" /> " + Environment.NewLine;
            }


            string fileContent = File.ReadAllText(vcxProjFilePath);
            fileContent = fileContent.Replace("<!--NEWFILES-->", nodeList);
            File.WriteAllText(vcxProjFilePath, fileContent);

        }

        private static void CreateVCXProjStructure(DirectoryInfo newDir,string projectName)
        {
            DirectoryInfo templateDir = new DirectoryInfo(Path.Combine(Application.StartupPath,"Template"));
            foreach (var file in templateDir.GetFiles()    )
            {
                string fileName = file.Name.Replace("ArduinoTemplate", projectName);
                var newFile = file.CopyTo(Path.Combine(newDir.FullName,fileName),true);

                string fileContent = File.ReadAllText(newFile.FullName);
                File.WriteAllText(newFile.FullName, fileContent.Replace("ArduinoTemplate", projectName));

            }
           


        }

        private static void UpdateInoFile(FileInfo newPdeFile, IEnumerable<FileInfo> pdeFiles, IEnumerable<FileInfo> inoFiles )
        {
            
            string header = File.ReadAllText(Path.Combine(Application.StartupPath,"header.txt"));
            string fileContent = File.ReadAllText(newPdeFile.FullName);
            fileContent = header + Environment.NewLine + fileContent;

            string additionalIncludes = "//Additional ino and pde files found in that directory." +Environment.NewLine;
            foreach (var fileInfo in pdeFiles)
            {
                if(fileInfo.Name != newPdeFile.Name)
                {
                    additionalIncludes += string.Format("#include \"{0}\"{1}", fileInfo.Name,Environment.NewLine);
                }
            }
            foreach (var fileInfo in inoFiles)
            {
                if (fileInfo.Name != newPdeFile.Name)
                {
                    additionalIncludes += string.Format("#include \"{0}\"{1}", fileInfo.Name, Environment.NewLine);
                }
            }

            fileContent = fileContent.Replace("<!--PDEFILES-->", additionalIncludes);


            File.WriteAllText(newPdeFile.FullName,fileContent);

        }
    }
}
